package jmine.tec.web.wicket.component.autocomplete;

import java.io.Serializable;
import java.util.Iterator;

import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.DefaultCssAutocompleteTextField;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.IAutoCompleteRenderer;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.model.IModel;

/**
 * AutoComplete genérico. Delega a tarefa de carregar as opções.
 * 
 * @author seiti Nov 16, 2011
 * @param <P> the type
 */
public class CustomAutocompleteTextField<P extends Serializable> extends AutoCompleteTextField<P> {

    private ChoicesLoader<P> loader;

    {
        add(CSSPackageResource.getHeaderContribution(DefaultCssAutocompleteTextField.class, "DefaultCssAutocompleteTextField.css"));
    }

    /**
     * Ctor
     * 
     * @param id id
     * @param model model
     * @param loader loader
     */
    public CustomAutocompleteTextField(String id, IModel<P> model, ChoicesLoader<P> loader) {
        super(id, model);
        this.loader = loader;
    }

    /**
     * Ctor
     * 
     * @param id id
     * @param loader loader
     */
    public CustomAutocompleteTextField(String id, ChoicesLoader<P> loader) {
        super(id);
        this.loader = loader;
    }

    /**
     * Ctor
     * 
     * @param id id
     * @param model model
     * @param renderer renderer
     * @param loader loader
     */
    public CustomAutocompleteTextField(String id, IModel<P> model, IAutoCompleteRenderer<P> renderer, ChoicesLoader<P> loader) {
        super(id, model, renderer);
        this.loader = loader;
    }

    /**
     * Ctor
     * 
     * @param id id
     * @param model model
     * @param persistable persistable
     * @param renderer renderer
     * @param settings settings
     * @param loader loader
     */
    public CustomAutocompleteTextField(String id, IModel<P> model, Class<P> persistable, IAutoCompleteRenderer<P> renderer,
            AutoCompleteSettings settings, ChoicesLoader<P> loader) {
        super(id, model, persistable, renderer, settings);
        this.loader = loader;

    }

    /**
     * C'tor
     * 
     * @param id {@link String}
     * @param choicesLoader the {@link ChoicesLoader}
     * @param renderer the {@link IAutoCompleteRenderer}
     */
    public CustomAutocompleteTextField(String id, ChoicesLoader<P> choicesLoader, IAutoCompleteRenderer<P> renderer) {
        this(id, (IModel<P>) null, renderer);
        this.loader = choicesLoader;
    }

    /**
     * Ctor
     * 
     * @param id id
     * @param model model
     * @param renderer renderer
     */
    protected CustomAutocompleteTextField(String id, IModel<P> model, IAutoCompleteRenderer<P> renderer) {
        super(id, model, renderer);
    }

    /**
     * Ctor
     * 
     * @param id id
     */
    protected CustomAutocompleteTextField(String id) {
        super(id);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Iterator<P> getChoices(String input) {
        return (Iterator<P>) this.loader.getChoices(input);
    }
}
